﻿using mds.DataAccessService;
using mds.ServiceInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace mds.ConfigurationService
{
    internal class MysqlConfigurationServiceProvider : IConfigurationService
    {
         private static MysqlConfigurationServiceProvider _instance = new MysqlConfigurationServiceProvider();

        public static IConfigurationService Instance
        {
            get { return _instance; }
           
        }
        private IDatabase _Database;
        private string _dataContring;
        private MysqlConfigurationServiceProvider()
        {
            _Database = DatabaseAdapter.Instance.CreateDatabase(EnumDatabaseType.MySql);
            _dataContring = Config.DatabaseConstring;
        }
        public BaseFrameModel.OperationResult<bool> CreateSolution(SoluationInfo newInfo)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            list.Add(new SqlDataParmeter()
            {
                IsPrimarykey = false,
                ParamterName = ""
            });
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.StoredProcedure, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> EditSolution(Guid SoluationId, string SoluationName, string SoluationSimple, bool Enable)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> DeleteSolution(Guid SoluationId)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> EnableSolutions(Guid[] SoluationId, bool Enable)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> DisableSolutions(Guid[] SoluationId, bool Enable)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public GridPage<SoluationInfo> GetSolustions(int pageSize, int currentIndex, string searchName)
        {
            InitObject<List<SoluationInfo>> ftpl = MysqlConfigurationServiceProvider.InitSoluations;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            List<SoluationInfo> list = _Database.GetData<List<SoluationInfo>>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new GridPage<SoluationInfo>(0, pageSize, list);
        }
        private static List<SoluationInfo> InitSoluations(IDataReader dr)
        {
            return default(List<SoluationInfo>);
        }
        public BaseFrameModel.OperationResult<SoluationInfo> GetSolution(Guid SoluationId)
        {
            InitObject<SoluationInfo> ftpl = MysqlConfigurationServiceProvider.InitSoluation;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            SoluationInfo data = _Database.GetData<SoluationInfo>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new BaseFrameModel.OperationResult<SoluationInfo>(data, data != null);
        }
        private static SoluationInfo InitSoluation(IDataReader dr)
        {
            return default(SoluationInfo);
        }
        public BaseFrameModel.OperationResult<bool> CreateApp(AppInfo newInfo)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> EditApp(Guid AppId, string AppName, string AppTitle)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> DeleteApp(Guid AppId)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public GridPage<AppInfo> GetApps(int pageSize, int currentIndex, string searchName)
        {
            InitObject<List<AppInfo>> ftpl = MysqlConfigurationServiceProvider.InitApps;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            List<AppInfo> list = _Database.GetData<List<AppInfo>>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new GridPage<AppInfo>(0, pageSize, list);
        }
        private static List<AppInfo> InitApps(IDataReader dr)
        {
            return default(List<AppInfo>);
        }

        public BaseFrameModel.OperationResult<AppInfo> GetApp(Guid AppId)
        {
            InitObject<AppInfo> ftpl = MysqlConfigurationServiceProvider.InitApp;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            AppInfo data = _Database.GetData<AppInfo>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new BaseFrameModel.OperationResult<AppInfo>(data, data != null);
        }
        private static AppInfo InitApp(IDataReader dr)
        {
            return default(AppInfo);
        }

        public BaseFrameModel.OperationResult<bool> CreateAppPlus(AppPlusInfo newInfo)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> EditAppPlus(Guid AppplusId, string AppPlusName, string AppTitle)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> DeleteAppPlus(Guid AppplusId)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public GridPage<AppPlusInfo> GetAppPluses(int pageSize, int currentIndex, string searchName)
        {
            InitObject<List<AppPlusInfo>> ftpl = MysqlConfigurationServiceProvider.InitAppPluses;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            List<AppPlusInfo> list = _Database.GetData<List<AppPlusInfo>>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new GridPage<AppPlusInfo>(0, pageSize, list);
        }

        private static List<AppPlusInfo> InitAppPluses(IDataReader dr)
        {
            return default(List<AppPlusInfo>);
        }

        public BaseFrameModel.OperationResult<AppPlusInfo> GetAppPlus(Guid AppId)
        {
            InitObject<AppPlusInfo> ftpl = MysqlConfigurationServiceProvider.InitAppPlus;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            AppPlusInfo data = _Database.GetData<AppPlusInfo>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new BaseFrameModel.OperationResult<AppPlusInfo>(data, data != null);
        }

        private static AppPlusInfo InitAppPlus(IDataReader dr)
        {
            return default(AppPlusInfo);
        }

        public BaseFrameModel.OperationResult<bool> CreateMdsConfigureInfo(MdsConfigureInfo newInfo)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> EditMdsConfigureInfo(Guid Id, string Content)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public BaseFrameModel.OperationResult<bool> DeleteMdsConfigureInfo(Guid Id)
        {
            string sql = "";
            List<SqlDataParmeter> list = new List<SqlDataParmeter>();
            return new BaseFrameModel.OperationResult<bool>(_Database.ExecuteNonQuery(_dataContring, sql, CommandType.Text, list) > 0);
        }

        public GridPage<MdsConfigureInfo> GetMdsConfigureInfos(int pageSize, int currentIndex, string searchName, Guid SoluationId)
        {
            InitObject<List<MdsConfigureInfo>> ftpl = MysqlConfigurationServiceProvider.InitMdsConfigures;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            List<MdsConfigureInfo> list = _Database.GetData<List<MdsConfigureInfo>>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new GridPage<MdsConfigureInfo>(0, pageSize, list);
        }

        private static List<MdsConfigureInfo> InitMdsConfigures(IDataReader dr)
        {
            return default(List<MdsConfigureInfo>);
        }

        public GridPage<MdsConfigureInfo> GetMdsConfigureInfosByAppplus(int pageSize, int currentIndex, string searchName, Guid AppplusId)
        {
            InitObject<List<MdsConfigureInfo>> ftpl = MysqlConfigurationServiceProvider.InitMdsConfigures;
            string sql = "";
            List<MdsConfigureInfo> list = null;
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            _Database.GetData<List<MdsConfigureInfo>>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new GridPage<MdsConfigureInfo>(0, pageSize, list);
        }

        public BaseFrameModel.OperationResult<MdsConfigureInfo> GetMdsConfigureInfo(Guid Id)
        {
            InitObject<MdsConfigureInfo> ftpl = MysqlConfigurationServiceProvider.InitMdsConfigure;
            string sql = "";
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            MdsConfigureInfo data = _Database.GetData<MdsConfigureInfo>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new BaseFrameModel.OperationResult<MdsConfigureInfo>(data, data != null);
        }

        private static MdsConfigureInfo InitMdsConfigure(IDataReader dr)
        {
            return default(MdsConfigureInfo);
        }
        public BaseFrameModel.OperationResult<List<MdsConfigureInfo>> GetMdsConfigureInfos(Guid SoluationId, int soluationVersion)
        {
            InitObject<List<MdsConfigureInfo>> ftpl = MysqlConfigurationServiceProvider.InitMdsConfigures;
            string sql = "";
            List<MdsConfigureInfo> list = null;
            List<SqlDataParmeter> sqlParms = new List<SqlDataParmeter>();
            _Database.GetData<List<MdsConfigureInfo>>(_dataContring, sql, CommandType.Text, sqlParms, ftpl);
            return new BaseFrameModel.OperationResult<List<MdsConfigureInfo>>(list, list != null);
        }
    }
}
